#include "../Include/link_pub.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Course* create_course(const char* id, const char* name, const char* type, int hours, int credits, int max_capacity) {
    Course* course = (Course*)malloc(sizeof(Course));
    if (!course) return NULL;
    
    strcpy(course->course_id, id);
    strcpy(course->course_name, name);
    strcpy(course->course_type, type);
    course->teaching_hours = hours;
    course->credits = credits;
    course->max_capacity = max_capacity;
    course->enrolled_count = 0;
    memset(course->enrolled_students, 0, sizeof(course->enrolled_students));
    course->next = NULL;
    
    return course;
}

Course* add_course(Course* head, Course* new_course) {
    if (!head) return new_course;
    
    Course* current = head;
    while (current->next) {
        current = current->next;
    }
    current->next = new_course;
    return head;
}

Course* delete_course(Course* head, Student* student_head, const char* course_id) {
    printf("DEBUG: Attempting to delete course: %s\n", course_id);
    
    if (!head) {
        printf("DEBUG: Course list is empty\n");
        return NULL;
    }
    
    // 检查头节点
    if (strcmp(head->course_id, course_id) == 0) {
        printf("DEBUG: Deleting head node\n");
        Course* to_delete = head;
        
        // 更新选了这门课的学生信息
        for (int i = 0; i < to_delete->enrolled_count; i++) {
            const char* student_id = to_delete->enrolled_students[i];
            if (strlen(student_id) == 0) continue;
            
            Student* student = student_head;
            while (student) {
                if (strcmp(student->student_id, student_id) == 0) {
                    // 从学生的选课列表中移除这门课
                    for (int j = 0; j < student->course_count; j++) {
                        if (strcmp(student->selected_courses[j], course_id) == 0) {
                            // 移动后续课程
                            for (int k = j; k < student->course_count - 1; k++) {
                                strcpy(student->selected_courses[k], student->selected_courses[k+1]);
                            }
                            student->course_count--;
                            student->total_credits -= to_delete->credits;
                            break;
                        }
                    }
                    break;
                }
                student = student->next;
            }
        }
        
        Course* new_head = head->next;
        free(to_delete);
        printf("DEBUG: Head course deleted successfully\n");
        return new_head;
    }
    
    // 查找非头节点
    Course* current = head->next;
    Course* prev = head;
    
    while (current) {
        if (strcmp(current->course_id, course_id) == 0) {
            printf("DEBUG: Found course to delete: %s\n", current->course_id);
            Course* to_delete = current;
            
            // 更新选了这门课的学生信息
            for (int i = 0; i < to_delete->enrolled_count; i++) {
                const char* student_id = to_delete->enrolled_students[i];
                if (strlen(student_id) == 0) continue;
                
                Student* student = student_head;
                while (student) {
                    if (strcmp(student->student_id, student_id) == 0) {
                        // 从学生的选课列表中移除这门课
                        for (int j = 0; j < student->course_count; j++) {
                            if (strcmp(student->selected_courses[j], course_id) == 0) {
                                // 移动后续课程
                                for (int k = j; k < student->course_count - 1; k++) {
                                    strcpy(student->selected_courses[k], student->selected_courses[k+1]);
                                }
                                student->course_count--;
                                student->total_credits -= to_delete->credits;
                                break;
                            }
                        }
                        break;
                    }
                    student = student->next;
                }
            }
            
            // 从链表中删除节点
            prev->next = current->next;
            free(to_delete);
            printf("DEBUG: Course deleted successfully\n");
            return head;
        }
        prev = current;
        current = current->next;
    }
    
    printf("DEBUG: Course %s not found in list\n", course_id);
    return head;
}

Course* modify_course(Course* head, const char* course_id, const char* name, const char* type, int hours, int credits, int max_capacity) {
    Course* course = find_course(head, course_id);
    if (course) {
        if (name) strcpy(course->course_name, name);
        if (type) strcpy(course->course_type, type);
        if (hours >= 0) course->teaching_hours = hours;
        if (credits >= 0) course->credits = credits;
        if (max_capacity >= 0) course->max_capacity = max_capacity;
        
        char log_msg[256];
        sprintf(log_msg, "Course %s modified", course_id);
        log_operation("MODIFY_COURSE", log_msg);
    }
    return head;
}

Course* find_course(Course* head, const char* course_id) {
    Course* current = head;
    while (current) {
        if (strcmp(current->course_id, course_id) == 0) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}

void print_course(Course* course) {
    if (!course) {
        printf("Course not found\n");
        return;
    }
    
    printf("Course ID: %s\n", course->course_id);
    printf("Name: %s\n", course->course_name);
    printf("Type: %s\n", course->course_type);
    printf("Teaching Hours: %d\n", course->teaching_hours);
    printf("Credits: %d\n", course->credits);
    printf("Max Capacity: %d\n", course->max_capacity);
    printf("Enrolled Students: %d/%d\n", course->enrolled_count, course->max_capacity);
    printf("Enrolled Students List:\n");
    
    for (int i = 0; i < course->enrolled_count; i++) {
        if (strlen(course->enrolled_students[i]) > 0) {
            printf("  - %s\n", course->enrolled_students[i]);
        }
    }
}

void print_all_courses(Course* head) {
    Course* current = head;
    printf("\n=== All Courses ===\n");
    if (!current) {
        printf("No courses available\n");
        return;
    }
    
    int count = 0;
    while (current) {
        printf("Course %d:\n", count);
        print_course(current);
        printf("-------------------\n");
        current = current->next;
        count++;
    }
}

int enroll_student(Course* course, const char* student_id) {
    if (!course || course->enrolled_count >= course->max_capacity) return -1;
    
    for (int i = 0; i < course->enrolled_count; i++) {
        if (strcmp(course->enrolled_students[i], student_id) == 0) {
            return -2;
        }
    }
    
    if (course->enrolled_count < 1000) {
        strcpy(course->enrolled_students[course->enrolled_count], student_id);
        course->enrolled_count++;
        return 0;
    }
    
    return -1;
}

int unenroll_student(Course* course, const char* student_id) {
    if (!course) return -1;
    
    for (int i = 0; i < course->enrolled_count; i++) {
        if (strcmp(course->enrolled_students[i], student_id) == 0) {
            for (int j = i; j < course->enrolled_count - 1; j++) {
                strcpy(course->enrolled_students[j], course->enrolled_students[j+1]);
            }
            course->enrolled_count--;
            return 0;
        }
    }
    
    return -1;
}

int is_course_full(Course* course) {
    return course->enrolled_count >= course->max_capacity;
}